import IHelper from '@/services/helper.service'
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, BeforeInsert, BeforeUpdate } from 'typeorm'

@Entity({name: "pub_task"})
export class PubTaskEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column('int', {nullable: true})
  uid: number

  @Column('int', {default: 0})
  pid: number

  @Column("varchar", {nullable: true})
  account: string

  @Column("varchar", {nullable: true})
  video_upload_id: string

  @Column("varchar", {nullable: true})
  video_bili_bvid: string

  @Column("int", {default: 1})
  video_type: string

  @Column('varchar', {length: 255, nullable: true})
  title: string

  @Column('text', {nullable: true})
  desc: string

  @Column('varchar', {nullable: true})
  bili_type: string

  @Column('varchar', {nullable: true})
  ks_type: string

  @Column("text", {nullable: true})
  common_tag: string

  @Column("text", {nullable: true})
  dy_tag: string

  @Column("int", {nullable: true})
  pub_state: number

  @Column('int', {default: 0, comment: `0: 未开始 1: 发布中 2 发布成功 3 发布失败`})
  status: number

  @CreateDateColumn({type: 'datetime'})
  createAt: Date

  @UpdateDateColumn({type: 'datetime'})
  updateAt: Date

  @BeforeUpdate()
  @BeforeInsert()
  setBvid() {
    this.video_bili_bvid = this.formatBvid(this.video_bili_bvid)
  }

  formatBvid(bvid: string | undefined) {
    if ((bvid || "").startsWith("http")) {
      return new URL(bvid).pathname.split("/").filter(e => e).pop()
    }
    return bvid
  }
}